package cn.tank.rabbitwork.service.impl;

import cn.tank.rabbitwork.dao.OrderRepository;
import cn.tank.rabbitwork.entity.Order;
import cn.tank.rabbitwork.service.IPayService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Optional;

@Service
public class PayServiceImpl implements IPayService {

    @Autowired
    private OrderRepository orderRepository;
    @Override
    public void payOrder(Integer orderId) {
        Optional<Order> byId = orderRepository.findById(orderId);
        if(byId.isEmpty()){
            throw new RuntimeException("订单不存在");
        }
        Order order = byId.get();
        if(order.getOrderStatus().getStatus()!=0){
            throw new RuntimeException("订单已关闭!");
        }

        order.setLastUpdateTime(new Date());
        //支付完成
        order.getOrderStatus().setStatus(1);
        orderRepository.save(order);
    }


}
